
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.query;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import logger.PoolLogger;

import property.Order;
import utility.CapitalChar;
import utility.ConvertToLower;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.GetIdName;
import file.SearchRecord;

public class In implements Queryinterface 
{
	String InObject;
	GetIdName gid;
	String buffer[];
	String Querybuffer[];
	String tablename;
	Hashtable<Object, Object > Querymap = new Hashtable<Object, Object>();
	String packagename="query.condition.";
	String key;
	String Message;
	String CONDITIONVALUE="conditionvalue";
	String CONDITIONFIELD="conditionfield";
	String LOGICALOPERATOR="logicaloperator";
	String QUERY="query";
	String FIELD="field";
	String CONDITIONOPERATOR="conditionoperator";	
	String StorageObject;
	String Key;
	String value;
	String QueryName;
	SearchRecord sr;
	String ReportName;		
	String typeofreport;
	Hashtable<Object, Object>Intable =new Hashtable<Object, Object>();
	Hashtable<Object, Object>sequencetable =new Hashtable<Object, Object>();
	String Query="SELECT distinct FROM";
	Order order;
	PoolLogger pl;
	
	public In() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public String[] executeQuery(){return null;}

	
	public void queryProcess(){
		try 
		{
			String conditionfield="";
			if(Intable.containsKey(CONDITIONFIELD))
			{
				 conditionfield = Intable.get(CONDITIONFIELD).toString();
				 Intable.remove(CONDITIONFIELD);
			}		
			if(!Intable.containsKey("condition"))
				Intable.put("condition", "");
			for(int i=0;i<sequencetable.size();i++)
			{	
				try
				{
					Queryinterface query;	
					String classname = sequencetable.get(Integer.toString(i+1)).toString();
					if(Intable.containsKey(classname.toLowerCase()))
					{
						Class c =Class.forName("query.query."+CapitalChar.makeFirstCharCapital(classname));
						query = (Queryinterface) c.newInstance();
						query.setbuffer(buffer);
						query.initPool();
						query.setObject(gid.getId(Intable.get(classname.toLowerCase()).toString()));
						query.settablename(tablename);
						tablename=query.gettablename();
						query.setbuffer(buffer);
						query.setQuerybuffer(Querybuffer);
						query.setKey(Key);
						Key = query.getkey();
						query.setValue(value);
						value = query.getvalue();
						query.setReportName(ReportName);
						query.setbuffer(buffer);
						query.setProcess("mod");
						query.setQuery(Query);
						query.setQuerybuffer(Querybuffer);
						query.settypeofreport(typeofreport);
						query.initializeData();
						query.settablename(tablename);						
						query.buildQuery();
						Query = query.getQuery();
						buffer  = query.getbuffer();
					}
				}
				catch (Exception e) 
				{
					e.printStackTrace();
				}
		    }
			Vector<Object> Conditionvector = new Vector<Object>();
			query.condition.Condition condition;				
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			condition = (query.condition.Condition)C.newInstance();
			condition.initCondition();
			condition.setConditionMap(Conditionvector);
			condition.getResult();
			Query =(condition.getQuery())+ " " +conditionfield +" IN " +  "( " +Query + " )";
		}	
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	

	
	@SuppressWarnings("unchecked")
	public void addReport() 
	{

		try 
		{
			String conditionfield="";
			if(Intable.containsKey(CONDITIONFIELD))
			{
				 conditionfield = Intable.get(CONDITIONFIELD).toString();
				 Intable.remove(CONDITIONFIELD);
			}			
			Enumeration<Object>enumeration = Intable.keys();
			while(enumeration.hasMoreElements())
			{
			try
			{
				Queryinterface query;				
				String classname = enumeration.nextElement().toString();
				Class c =Class.forName("query.query."+CapitalChar.makeFirstCharCapital(classname));
				query = (Queryinterface) c.newInstance();
				query.setObject(gid.getId(Intable.get(classname).toString()));
				query.settablename(tablename);
				tablename=query.gettablename();
				query.setbuffer(buffer);
				query.setQuerybuffer(Querybuffer);
				query.setKey(Key);
				Key = query.getkey();
				query.setValue(value);
				query.initPool();
				value = query.getvalue();
				query.setReportName(ReportName);
				query.setbuffer(buffer);
				query.setProcess("add");
				query.setQuerybuffer(Querybuffer);
				query.settypeofreport(typeofreport);
				query.initializeData();
				query.settablename(tablename);
				query.buildQuery();
				Query = query.getQuery();
			}
			catch (Exception e) 
			{	e.printStackTrace();
			}
			Vector<Object> Conditionvector = new Vector<Object>();
			
			query.condition.Condition condition;				
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			condition = (query.condition.Condition)C.newInstance();
			condition.initCondition();
			condition.setConditionMap(Conditionvector);
			condition.getResult();
			Query =(condition.getQuery())+ " IN " + conditionfield +Query;
		  }
		}	
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	
	}

	public Hashtable<Object, Object> getQueryMap() 
	{
		return Querymap;
	}

	public String getQueryName() 
	{
		return null;
	}

	public String[] getQuerybuffer() 
	{
		return Querybuffer;
	}

	public String getReportname() 
	{
		return ReportName;
	}

	public void getResult() 
	{	
		
	}

	public String[] getbuffer()
	{
		return buffer;
	}

	public String getkey() 
	{
		return key;
	}

	public String gettablename()
	{
		return tablename;
	}

	public String gettid() 
	{
		return null;
	}

	public String getvalue() 
	{
		return value;
	}

	public void initializeData() 
	{	
		
		RecordSearch rs =new RecordSearch();		
		Intable.put("mid",InObject);
		Intable.put("td", "null");
		rs.setConditionMap(Intable);
		String line[] = rs.getArrayFromResultSet();
		Intable.clear();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = splitline[Column.pv_index-1];
			if(value.equals("0"))
				value = splitline[Column.vt_index-1];
			else
				value = gid.getItem(value);
			Intable.put(gid.getItem(splitline[Column.pid_index-1]), value);
		}
		Intable = ConvertToLower.convertHashKey(Intable);
		
		sequencetable = order.getSequences(QUERY);
		
	}

	public void initPool() 
	{
		gid= new GetIdName();
		FIELD = gid.getId(FIELD);
		QUERY = gid.getId(QUERY);
		order = new Order();
	}

	public void setKey(String key) 
	{
		this.key =key;
	}

	public void setObject(String object) 
	{
		this.InObject = object;
	}

	public void setQueryMap(Hashtable<Object, Object> QueryMap) 
	{
		this.Querymap.putAll(QueryMap);
	}

	public void setQueryName(String QueryName) 
	{
		
	}

	public void setQuerybuffer(String[] Querybuffer) 
	{
		this.Querybuffer = Querybuffer; 
	}

	public void setReportName(String Reportname) 
	{
		this.ReportName  =Reportname;
	}

	public void setReportmap(Hashtable<Object, Object> ReportMap) 
	{
		
	}

	public void setValue(String value) 
	{
		this.value = value;
	}

	public void setViewObject(String object, char ch) 
	{
		
	}

	public void setbuffer(String[] Querybuffer) {
	
		
	}

	public void setreporton(String reporton) 
	{
		
	}

	public void settablename(String tablename) 
	{
		this.tablename = tablename;
	}

	public void settid(String tid) 
	{
		
	}

	public void settypeofreport(String typeofreport) 
	{
		this.typeofreport = typeofreport;
	}

	@SuppressWarnings("unchecked")
	public void updateReport() 
	{

		try 
		{
			String conditionfield="";
			if(Intable.containsKey(CONDITIONFIELD))
			{
				 conditionfield = Intable.get(CONDITIONFIELD).toString();
				 Intable.remove(CONDITIONFIELD);
			}		
			if(!Intable.containsKey("condition"))
				Intable.put("condition", "");
			for(int i=0;i<sequencetable.size();i++)
			{	
				try
				{
					Queryinterface query;	
					String classname = sequencetable.get(Integer.toString(i+1)).toString();
					if(Intable.containsKey(classname.toLowerCase()))
					{
						Class c =Class.forName("query.query."+CapitalChar.makeFirstCharCapital(classname));
						query = (Queryinterface) c.newInstance();
						query.setbuffer(buffer);
						query.initPool();
						query.setObject(gid.getId(Intable.get(classname.toLowerCase()).toString()));
						query.settablename(tablename);
						tablename=query.gettablename();
						query.setbuffer(buffer);
						query.setQuerybuffer(Querybuffer);
						query.setKey(Key);
						Key = query.getkey();
						query.setValue(value);
						value = query.getvalue();
						query.setReportName(ReportName);
						query.setbuffer(buffer);
						query.setProcess("mod");
						query.setQuery(Query);
						query.setQuerybuffer(Querybuffer);
						query.settypeofreport(typeofreport);
						query.initializeData();
						query.settablename(tablename);						
						query.buildQuery();
						Query = query.getQuery();
						buffer  = query.getbuffer();
					}
				}
				catch (Exception e) 
				{
					e.printStackTrace();
				}
		    }
			Vector<Object> Conditionvector = new Vector<Object>();
			query.condition.Condition condition;				
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			condition = (query.condition.Condition)C.newInstance();
			condition.initCondition();
			condition.setConditionMap(Conditionvector);
			condition.getResult();
			Query =(condition.getQuery())+ " " +conditionfield +" IN " +  "( " +Query + " )";
		}	
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	
	}

	@SuppressWarnings("unchecked")
	public void setViewObject(Vector<Object> PropertyMap) 
	{
		try 
		{
			this.Intable.putAll((Map<? extends Object, ? extends Object>) PropertyMap.get(0));
		} catch (Exception e) 
		{
		}
	}

	public String getQuery()
	{
		return Query;
	}

	
	public int getcount() {
		// TODO Auto-generated method stub
		return 0;
	}

	
	public void setCount(int count) {
		// TODO Auto-generated method stub
		
	}

	
	public void setMaxcount(int count) {
		// TODO Auto-generated method stub
		
	}

	
	public void setQuery(String QueryName) {
		// TODO Auto-generated method stub
		
	}

	
	public void buildQuery() {
		// TODO Auto-generated method stub
		
	}
	public void setConditionMap(Vector<Object> QueryMap)
	{
		
	}
	
	public void setProcess(String process) {
		// TODO Auto-generated method stub
		
	}
}
